package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/aluminumsmelting/api")
@AllArgsConstructor
public class AluminumSmeltingController {

    private final HttpServletRequest httpServletRequest;
    private final AluminumSmeltingService aluminumSmeltingService;
    private final AluminumSmeltingDetailService aluminumSmeltingDetailService;
    private final AluminumSmeltingCarnonateService aluminumSmeltingCarnonateService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(AluminumSmelting aluminumSmelting) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getNcv())) {
                ncvs = Arrays.asList(aluminumSmelting.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getFc())) {
                fcs = Arrays.asList(aluminumSmelting.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getCc())) {
                ccs = Arrays.asList(aluminumSmelting.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getOf())) {
                ofs = Arrays.asList(aluminumSmelting.getOf().split(","));
            }
            List<String> adCarbonates = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getAdCarbonates())) {
                adCarbonates = Arrays.asList(aluminumSmelting.getAdCarbonates().split(","));
            }
            List<String> efCarbonates = new ArrayList<>();
            if (StringUtils.isNotBlank(aluminumSmelting.getEfCarbonates())) {
                efCarbonates = Arrays.asList(aluminumSmelting.getEfCarbonates().split(","));
            }

            Double eBurn = AluminumSmeltingUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eRawMaterial = AluminumSmeltingUtil.getERawMaterial(aluminumSmelting.getP());
            Double eProcess = AluminumSmeltingUtil.getEProcess(aluminumSmelting.getP(), adCarbonates, efCarbonates, aluminumSmelting.getQ(), aluminumSmelting.getPur());
            Double ePurchaseElectric = AluminumSmeltingUtil.getEPurchaseElectric(aluminumSmelting.getAdPurchaseElectric(), aluminumSmelting.getEfPurchaseElectric());
            Double ePurchaseHeat = AluminumSmeltingUtil.getEPurchaseHeatOrEOutIputHeat(aluminumSmelting.getAdPurchaseHeat());
            Double eOutIputHeat = AluminumSmeltingUtil.getEPurchaseHeatOrEOutIputHeat(aluminumSmelting.getAdOutInputHeat());
            Double eOutIputElectric = AluminumSmeltingUtil.getEOutputElectric(aluminumSmelting.getAdOutInputElectric(), aluminumSmelting.getEfOutInputElectric());
            Double result = AluminumSmeltingUtil.getE(eBurn, eRawMaterial, eProcess, ePurchaseElectric, ePurchaseHeat, eOutIputElectric, eOutIputHeat);

            aluminumSmelting.setCustomerId(customer.getId());
            aluminumSmelting.setCreateTime(new Date());
            aluminumSmelting.setEBurn(GasDCUtil.round(eBurn, 2));
            aluminumSmelting.setEProcess(GasDCUtil.round(eProcess, 2));
            aluminumSmelting.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            aluminumSmelting.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            aluminumSmelting.setEOutInputHeat(GasDCUtil.round(eOutIputHeat, 2));
            aluminumSmelting.setEOutInputElectric(GasDCUtil.round(eOutIputElectric, 2));
            aluminumSmelting.setState(Constants.STATE_INVAILD);
            aluminumSmelting.setResult(GasDCUtil.getThousandth(result));
            aluminumSmeltingService.save(aluminumSmelting);
            for (int i = 0; i < ncvs.size(); i++) {
                AluminumSmeltingDetail aluminumSmeltingDetail = new AluminumSmeltingDetail();
                aluminumSmeltingDetail.setNcv(Double.valueOf(ncvs.get(i)));
                aluminumSmeltingDetail.setFc(Double.valueOf(fcs.get(i)));
                aluminumSmeltingDetail.setCc(Double.valueOf(ccs.get(i)));
                aluminumSmeltingDetail.setOf(Double.valueOf(ofs.get(i)));
                aluminumSmeltingDetail.setAluminumSmeltingId(aluminumSmelting.getId());
                aluminumSmeltingDetailService.save(aluminumSmeltingDetail);
            }
            for (int i = 0; i < adCarbonates.size(); i++) {
                AluminumSmeltingCarbonate aluminumSmeltingCarbonate = new AluminumSmeltingCarbonate();
                aluminumSmeltingCarbonate.setAdCarbonate(Double.valueOf(adCarbonates.get(i)));
                aluminumSmeltingCarbonate.setEfCarbonate(Double.valueOf(efCarbonates.get(i)));
                aluminumSmeltingCarbonate.setAluminumSmeltingId(aluminumSmelting.getId());
                aluminumSmeltingCarnonateService.save(aluminumSmeltingCarbonate);
            }
            return ResultUtil.success("计算成功", aluminumSmelting);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
